package jvlite.dao.JliteORM;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class JliteJdbcSupport extends AbsJliteJdbcFacade {

	public JliteJdbcSupport(){
		super(new JliteSessionMgr());
	}
	
	protected JliteJdbcSupport(JliteSessionMgr mgr) {
		super(mgr);
	}

	public static Integer update(String sql, Object[] params)
			throws SQLException {
		Connection conn = null;
		PreparedStatement st = null;
		ResultSet rs = null;
		Integer rows = null;
		try {
			// conn = DBPool.getConnection();
			conn = JliteDataSource.getConnection();
			st = conn.prepareStatement(sql);
			for (int i = 0; i < params.length; i++) {
				st.setObject(i + 1, params[i]);
			}
			rows = st.executeUpdate();
		} finally {
			// DBPool.release(conn, st, rs);
			JliteDataSource.release(null, st, rs);
		}
		return rows;
	}

	// distill the similar code of CURD, optimize query��replace the other query
	// method
	public static Object query(String sql, Object[] params,
			IResultSetHandler handler) throws SQLException {

		Connection conn = null;
		PreparedStatement st = null;
		ResultSet rs = null;

		try {
			conn = JliteDataSource.getConnection();
			st = conn.prepareStatement(sql);
			for (int i = 0; i < params.length; i++) {
				st.setObject(i + 1, params[i]);
			}

			/*
			 * the skeleton call the interface to deal with result set export an
			 * interface to allow the user to create some object to implement
			 * the function of handling result set
			 */
			rs = st.executeQuery();

			return handler.handler(rs);

		} finally {
			// DBPool.release(conn, st, rs);
			JliteDataSource.release(null, st, rs);
		}

	}

	// distill the similar code of CURD, optimize query��replace the other query
	// method
	public static String queryBySQL(String sql) throws SQLException {

		Connection conn = null;
		Statement st = null;
		ResultSet rs = null;

		String tableName = "";
		try {
			conn = JliteDataSource.getConnection();
			st = conn.createStatement();
			rs = st.executeQuery(sql);
			while (rs.next()) {
				tableName = rs.getString(1);
			}
			return tableName;

		} finally {
			// DBPool.release(conn, st, rs);
			JliteDataSource.release(null, st, rs);
		}

	}

	// withdraw the similar code of CURD, optimize query��replace the other
	// query method
	public static Object queryBySQL(String sql, IResultSetHandler handler)
			throws SQLException {

		Connection conn = null;
		Statement st = null;
		ResultSet rs = null;

		try {
			conn = JliteDataSource.getConnection();
			st = conn.createStatement();
			rs = st.executeQuery(sql);
			return handler.handler(rs);
		} finally {
			// DBPool.release(conn, st, rs);
			JliteDataSource.release(null, st, rs);
		}

	}

	// withdraw the similar code of CURD, optimize query replace the other
	// query method
	public static void executeSQL(String sql) throws SQLException {

		Connection conn = null;
		PreparedStatement st = null;
		ResultSet rs = null;

		try {
			conn = JliteDataSource.getConnection();
			st = conn.prepareStatement(sql);
			st.execute();
		} finally {
			// DBPool.release(conn, st, rs);
			JliteDataSource.release(null, st, rs);
		}

	}

}